﻿namespace Compass.Maui.ViewModels;

public partial class AppShellViewModel:BaseViewModel
{
    private readonly ILoginService _loginService;

    public AppShellViewModel(ILoginService loginService,IPreferenceService preferenceService)
    {
        _loginService = loginService;
        UserName = preferenceService.Get(Consts.UserName, "");
        //4.响应登录事件，处理事件的参数
        loginService.Logged += LoginServiceOnLogged;
    }
    //todo:该字段显示在浮出控件头部文字上不会更新，Model=TwoWay也没用
    private void LoginServiceOnLogged(object? sender, UserLoggedEventArgs e)
    {
        UserName = e.LoggedUser.UserName;
    }
    [ObservableProperty] private string userName;

    //绑定到浮出控件底部的Logout Button
    [RelayCommand]
    private async Task LogoutAsync()
    {
        await IsBusyFor(async () =>
        {
           await _loginService.LogoutAsync();
           await Shell.Current.GoToAsync($"//{nameof(LoginPage)}");
        });
    }
}